package com.demomaster.quickcompat.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.LayoutRes;

import com.demomaster.quickcompat.R;

import java.util.List;

public class SimpleRecycleAdapter extends QuickRecyclerAdapter<SimpleRecycleAdapter.ViewHolder> {

    private List data = null;
    private Context context;
    private @LayoutRes
    int layoutResId = R.layout.item_option_menu;

    public SimpleRecycleAdapter(Context context, List lists) {
        this.context = context;
        this.data = lists;
    }

    public SimpleRecycleAdapter(Context context, @LayoutRes int layoutResId, List lists) {
        this.context = context;
        this.data = lists;
        this.layoutResId = layoutResId;
    }

    //创建View,被LayoutManager所用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(layoutResId, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    //数据的绑定
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        //holder.textView.setTextSize(60);
        super.onBindViewHolder(holder, position);
        onBindView(holder, data, position);
    }

    public void onBindView(ViewHolder holder, List data, int position) {
        View view = holder.itemView.findViewById(R.id.tv_title);
        if(view instanceof TextView){
            ((TextView)view).setText(position+"");
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    //自定义ViewHolder,包含item的所有界面元素
    public static class ViewHolder extends QuickViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
